package com.bosssoft.hr.train.j2se.collection;

import com.bosssoft.hr.train.j2se.pojo.entity.User;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * User对象相同时，hashSet能否过滤，这意味这重写User的hashcode和equal
 *
 * @author 史旭升
 * @date 2022-12-06 23:21
 */
public class HashSetDemo {
    /**
     * 使用HashSet存储10个User
     */
    private static Set<User> userHashSet=new HashSet<>();

    /**
     *main方法调用usersEntrySet（）遍历集合
     * @author shi xusheng
     * @date 2022/12/06 22:52
     * @param args
     * @return void
     */
    public static void main(String[] args) {
        int num=10;
        User[] users=new User[num];
        for(int i=0;i<num-1;i++){
            users[i]=new User(
                    new Long(i),
                    "史旭升"+i
            );
            userHashSet.add(users[i]);
        }
        //添加第十个对象和第九个一致
        User user=new User(
                new Long(8),
                "史旭升"+8
        );
        userHashSet.add(user);
        //nu是统计对象个数的参数
        int nu=1;
        new HashSetDemo().usersIterator(nu);
    }

    /**
     *使用迭代器遍历userHashSet
     * @author shi xusheng
     * @date 2022/12/06 23:00
     * @return void
     */
    public void usersIterator(int num){
        Iterator<User> userIterator=userHashSet.iterator();
        while (userIterator.hasNext()){
            int num1=num++;
            System.out.println(userIterator.next().toString());
            System.out.println(num1);
        }
    }
}
